<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      title="文章详情"
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading v-if="loading" class="article-loading" color="#1989fa" type="spinner" />
    <!-- /加载中 loading -->

    <!-- 文章详情 -->
    <div v-else class="detail">
      <h3 class="title">{{ article.title }}</h3>
      <div class="author">
        <van-image :src="article.aut_photo" round width="1rem" height="1rem" fit="fill" />
        <div class="text">
          <p class="name">{{ article.aut_name }}</p>
          <p class="time">{{ article.pubdate | relativeTime }}</p>
        </div>
        <!-- 互斥渲染两个按钮 is_followed 为true 显示取关按钮  为false显示关注按钮 -->
        <van-button
          v-if="article.is_followed"
          round
          size="small"
          type="danger"
          @click="follow"
        > 取 关</van-button>
        <van-button
          v-else
          round
          size="small"
          type="info"
          @click="follow"
        >+ 关注</van-button>
      </div>
      <div class="content">
        <div v-html="article.content" />
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button round size="small" hairline type="primary" plain icon="good-job-o">点赞</van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button round size="small" hairline type="danger" plain icon="delete">不喜欢</van-button>
      </div>
      <!-- 评论列表组件 -->
      <div>
        <article-comment :id="article.art_id.toString()" />
      </div>
    </div>

    <!-- /文章详情 -->
  </div>
</template>

<script>
// 路由地址 xxx?key=value  query传参  this.$route.query.xxx
// 路由地址 xxx/id值  params传参  this.$route.params.xxx
import { reqGetArticleInfo } from '@/api/article'
import { reqFollowUser, reqUnFollowUser } from '@/api/user'
import ArticleComment from './components/article-comment'
export default {
  name: 'ArticleIndex',
  components: { ArticleComment },
  data() {
    return {
      loading: true, // 控制加载中的 loading 状态
      article: { }
    }
  },
  // beforeCreate() {
  //   console.log('fa beforeCreate')
  // },
  created() {
    this.getArticleInfo()
    // console.log('fa created')
  },
  // beforeMount() {
  //   console.log('fa beforeMount')
  // },
  // mounted() {
  //   console.log('fa mounted')
  // },
  methods: {
    // 获取文章详情的方法
    async getArticleInfo() {
      const res = await reqGetArticleInfo(this.$route.params.id)
      console.log(res)
      // 请求结束后将布尔值设置为false
      this.loading = false
      this.article = res.data.data
    },
    // 关注和取关的方法
    follow() {
      //  根据 is_followed的值来判断  如果为false 发送的是关注的请求, true 取关的请求
      // console.log(this.article.is_followed)
      if (this.article.is_followed) {
        // 取关
        reqUnFollowUser(this.article.aut_id) // 取关后页面如果有报错 看一下requesjs transformResponse是否有try catch
        // this.article.is_followed = false
        this.$toast.success('取关成功')
      } else {
        // 关注
        reqFollowUser(this.article.aut_id)
        // this.article.is_followed = true
        this.$toast.success('关注成功')
      }
      // 直接更新视图
      this.article.is_followed = !this.article.is_followed
    }
  }
}
</script>

<style lang='scss' scoped>
.article-container{
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error{
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan{
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size:14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    ::v-deep img{
      max-width:100%;
      background: #f9f9f9;
    }
  }
}
</style>
